package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.Warehousing;
import com.hqyj.dao.WarehousingMapper;
import com.hqyj.service.IWarehousingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 入库执行记录 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class WarehousingServiceImpl implements IWarehousingService {

    //注入purchaseMapper
    @Autowired(required = false)
    WarehousingMapper warehousingMapper;

    //加载分页
    @Override
    public HashMap<String, Object> select(Warehousing warehousing) {
        //创建HashMap对象
        HashMap<String,Object> map = new HashMap<>();
        //创建分页对象
        Page<Warehousing> page = new Page<>(warehousing.getPage(),warehousing.getRow());

        //定义条件构造器
        QueryWrapper<Warehousing> queryWrapper = new QueryWrapper<>();
        //查询数据库
        Page<Warehousing> rowsPageList = warehousingMapper.selectPage(page, queryWrapper);
        //设置时间
        List<Warehousing> records = rowsPageList.getRecords();
        for (Warehousing record : records) { ;
            record.setWTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(record.getWarehousingTime()));
        }
        //定义当前页集合
        map.put("list",rowsPageList.getRecords());
        //总页数
        map.put("totalPage",rowsPageList.getPages());
        //总条数
        map.put("total",rowsPageList.getTotal());
        //当前页
        map.put("curPage",warehousing.getPage());
        //如果当前页为1，则上一页为也为1
        if (rowsPageList.getCurrent() == 1){
            //上一页
            map.put("prePage",1);
        }else {
            map.put("prePage",warehousing.getPage()-1);
        }
        //如果当前页为最后一页，则下一页为也为最大页
        if (warehousing.getPage() == rowsPageList.getPages()){
            //下一页
            map.put("nextPage",rowsPageList.getPages());
        }else {
            map.put("nextPage",warehousing.getPage()+1);
        }
        //显示每页条数
        map.put("row",warehousing.getRow());
        return map;
    }


    @Override
    public HashMap<String, Object> del(Warehousing warehousing) {
        //创建HashMap对象
        HashMap<String,Object> map = new HashMap<>();
        int num = warehousingMapper.deleteById(warehousing.getWarehousingId());
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }
}
